<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    request.setAttribute("basePath",basePath);
%>
<%@ include file="/resources/js/easyui/taglibs.jsp"%>
<%@ include file="/resources/js/wechat/_wechatui.jsp"%>
<!DOCTYPE>
<html>
	<head>	
		<title>绑定</title>
		<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
		
	</head>
	<body>
		<div class="loading"></div>
		<!-- header -->
		<header>
			<a href="javascript:history.go(-1);" class="top_fh">绑定</a>
		</header>
		<!-- header end -->
		
		<div class="page">
			<div class="homecdbj_inner">
				<div class="bangding">
					<ul>
					<form id="myForm" name="myForm">
					
						<li><p>
                        	<span>设备简码</span>
                            <a id="scbutton" href="javascript:;">扫一扫</a>
                            <input id="shortCode" style="margin-right:4rem;text-align:left;" type="text" value="">
							<input  name="shortCode" type="hidden" id="shortCode_hidden" />
						</p></li>
						<li><p>
                        	<span>IMEI/MAC码</span>
                            <a id="immebutton"href="javascript:;">扫一扫</a>
                            <input id="imme"  style="margin-right:4rem;text-align:left;" type="text" value="">
							<input name="imme" type="hidden" id="imme_hidden" />
						</p></li>
						<li><p>
                        	<span>支付码</span>
                            <a  id="pdbutton" href="javascript:;">扫一扫</a>
                            <input id="payCode" style="margin-right:4rem;text-align:left;" type="text" value="">
							<input name="payCode" type="hidden" id="payCode_hidden" />
						</p></li>
                      </form>
					</ul>

					<div class="tcse_button bangdingbtn"><a href="JavaScript:;" style="border-radius:3px;">绑定</a></div>
				</div>
			</div>
		</div>
		<!--  <div class="ty_pop bangding" style="display:none;">
			<div class="popbg"></div>
			<div class="lxts_inner" style="height:10rem; margin-top: -5rem;">
				<p style="margin-top:1rem;"><img src="${basePath}resources/images/wechat/images/queren.png" alt=""></p>
				<p>绑定成功！</p>
                <!-- <p style="margin-top:1rem;"><img src="images/ico_tsgt.png" alt=""></p>
				<p>绑定失败！</p> -->
			</div>
		</div>
	</body>
</html>

<script>

	wx.config({
	    debug: false,
	    appId: '${jsret.appid}',
	    timestamp:'${jsret.timestamp}',
	    nonceStr: '${jsret.nonceStr}',
	    signature: '${jsret.signature}',
	    jsApiList: [
	      'scanQRCode'
	    ]
	});
	wx.ready(function(){
		$("#scbutton").on("click", function(){
			wx.scanQRCode({
			    needResult: 1,
			    scanType: ["qrCode","barCode"],
			    success: function (res) {

			   		 var result = res.resultStr;

			   		 $('#shortCode').val(result);
			   		 $('#shortCode_hidden').val(result);
					//$("#shortCode").attr("disabled","disabled");

				}
			});
		});
		$("#immebutton").on("click", function(){
			wx.scanQRCode({
			    needResult: 1,
			    scanType: ["qrCode","barCode"],
			    success: function (res) {
					var result = res.resultStr;
					if(result.lastIndexOf('IMEI:')>0&&result.lastIndexOf('SW:')>0){
							//result=result.substring( result.lastIndexOf('IMEI:')+5,result.lastIndexOf('SW:')-1);
						result=result.substring( result.lastIndexOf('IMEI:')+5,result.lastIndexOf('IMEI:')+20);
						//alert(result);
						//alert(result.substring( result.lastIndexOf('IMEI:')+5,result.lastIndexOf('IMEI:')+20));

					}
					$('#imme').val(result);
					$('#imme_hidden').val(result);
					//$("#imme").attr("disabled","disabled");

				}
			});
		});

		$("#pdbutton").on("click", function(){
			wx.scanQRCode({
			    needResult: 1,
			    scanType: ["qrCode","barCode"],
			    success: function (res) {
			   		 var result = res.resultStr;
					 if(result.lastIndexOf('/pay/')<0){
						 alert('支付码不正确');
						 return ;
					 }
					if(result.lastIndexOf('/pay')<0){
						alert('支付码不正确');
						return ;
					}
					var val=result.substring( result.lastIndexOf('/pay/')+5,result.lastIndexOf('/pay'));

					$('#payCode').val(val);
					$('#payCode_hidden').val(val);

					//$("#payCode").attr("disabled","disabled");

				}
			});
		});

	});

$(function(){
	//登录
	$(".tcse_button a").on("click", function(){
		var shortCode = $("#shortCode_hidden").val();
		var imme = $("#imme_hidden").val();
		var payCode = $("#payCode_hidden").val();
		if(payCode==null||payCode=='') {
			payCode = $("#payCode").val();
			$("#payCode_hidden").val(payCode);
		}
		if(imme==null||imme=='') {
			imme = $("#imme").val();
			$("#imme_hidden").val(imme);
		}
		if(shortCode==null||shortCode=='') {
			shortCode = $("#shortCode").val();
			$("#shortCode_hidden").val(shortCode);
		}
		if(shortCode==null||shortCode==''){
				alert("设备简码不能为空");
		}else if(imme==null||imme==''){
			alert("IMEI/MAC码不能为空");
		}else if(payCode==null||payCode==''){
			alert("设备码不能为空");
		}else{
			$.ajax({
				url:'${basePath}wechat/bind/bind/add',
				type : 'POST',
				data : $("#myForm").serialize(),
				error: function (response) {
				//	ViewHelper.func.ajaxWechatErrorMessage(response);
				},
				success: function (ret) {
					if(ret.result) {
						alert('绑定成功！');
					}else{
						alert(ret.info);
					}
				}
			}); 
			
		}
				
	});

});

</script>

